package com.count.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.p.Hands;
import com.p.P;

public class G {
	public G(String myId) {
		this.myId = myId;
	}

	private GTextInfo ginfo = new GTextInfo();
	private Map<String, PlayerInfo> playerInfoMap = new HashMap<String, PlayerInfo>();

	public Map<String, PlayerInfo> getPlayerInfoMap() {
		return playerInfoMap;
	}

	private int playerNumber;
	private Hands myHands = new Hands();
	private List<P> flopPList;
	private List<P> turnPList;
	private List<P> riverPList;

	public GTextInfo getGinfo() {
		return ginfo;
	}

	private void setPlayerPreflopInfo(String preflopStr) {
		int i = 1;
		int betCount = 0;
		for (String s : preflopStr.split("\r\n")) {
			String playerName;

			if (s.indexOf("Seat " + i) > -1) {
				playerName = s.substring(s.indexOf("Seat " + i + ": ") + ("Seat " + i + ": ").length(), s.indexOf("($"));
				playerName = playerName.trim();
				PlayerInfo pinfo = new PlayerInfo();
				pinfo.setName(playerName);
				playerInfoMap.put(playerName, pinfo);
				i++;
			} else {
				for (PlayerInfo pi : playerInfoMap.values()) {
					if (s.indexOf(pi.getName() + ": ") > -1) {
						if (s.indexOf(" folds ") > -1) {
							pi.setPreflopFold(betCount);
						} else if (s.indexOf(" posts big blind $") > -1) {
							betCount = 1;
							pi.setPreflopBetTimes(betCount);
							pi.setBigBlind(true);
						} else if (s.indexOf(" posts small blind $") > -1) {
							pi.setSmallBlind(true);
						} else if (s.indexOf(" raises ") > -1) {
							betCount++;
							pi.setPreflopBetTimes(betCount);
						} else if (s.indexOf(" calls ") > -1) {
							pi.setPreflopBetTimes(betCount);
						}
					}
				}
			}
		}
	}

	public void process() {
		setMyHands(ginfo.getPreflopStr());

		setPlayerPreflopInfo(ginfo.getPreflopStr());

		if (ginfo.getFlopStr() != null) {
			setFlopPList(ginfo.getFlopStr());
		}
		if (ginfo.getTurnStr() != null) {
			setTurnPList(ginfo.getTurnStr());
		}
		if (ginfo.getRiverStr() != null) {
			setRiverPList(ginfo.getRiverStr());
		}
		if (ginfo.getSummaryStr().indexOf(" showed ") > -1) {
			setPlayerHands(ginfo.getSummaryStr());
		}
		if (ginfo.getSummaryStr().indexOf(" grinder_2015 (small blind) collected ") > -1 || ginfo.getSummaryStr().indexOf(" grinder_2015 (big blind) collected ") > -1
				|| ginfo.getSummaryStr().indexOf(" grinder_2015 collected ") > -1) {

			for (String line : ginfo.getSummaryStr().split("\r\n")) {
				if (line.indexOf("collected") > -1) {
					winMoney = Double.parseDouble(line.substring(line.indexOf("collected") + 12, line.length() - 1));
				}
			}
		}
	}

	private double winMoney;

	private void setPlayerHands(String summaryStr) {
		for (String s : summaryStr.split("\r\n")) {
			if (s.indexOf(" showed [") > -1 && s.indexOf(myId) == -1) {
				int idx = s.indexOf(" showed [") + (" showed [").length();
				String player = s.substring(8, s.indexOf(" showed [")).replace(" (small blind)", "").replace(" (big blind)", "").replace(" (button)", "");
				for (int i = 0; i < 2; i++) {
					String pStr = s.substring(idx + i * 3, idx + i * 3 + 2);
					P p = new P();
					p.setNums(pStr.substring(0, 1));
					p.setColors(pStr.substring(1, 2));
					if (i == 0) {
						playerInfoMap.get(player).getHands().setP1(p);
					} else {
						playerInfoMap.get(player).getHands().setP2(p);
					}
				}
			} else if (s.indexOf(" mucked [") > -1 && s.indexOf(myId) == -1) {
				int idx = s.indexOf(" mucked [") + (" mucked [").length();
				String player = s.substring(8, s.indexOf(" mucked [")).replace(" (small blind)", "").replace(" (big blind)", "").replace(" (button)", "");
				for (int i = 0; i < 2; i++) {
					String pStr = s.substring(idx + i * 3, idx + i * 3 + 2);
					P p = new P();
					p.setNums(pStr.substring(0, 1));
					p.setColors(pStr.substring(1, 2));
					if (i == 0) {
						playerInfoMap.get(player).getHands().setP1(p);
					} else {
						playerInfoMap.get(player).getHands().setP2(p);
					}
				}
			}
		}
	}

	private String myId;

	public void setMyId(String myId) {
		this.myId = myId;
	}

	private void setMyHands(String s) {
		int idx = s.indexOf("Dealt to " + myId + " [") + ("Dealt to " + myId + " [").length();
		for (int i = 0; i < 2; i++) {
			String pStr = s.substring(idx + i * 3, idx + i * 3 + 2);
			P p = new P();
			p.setNums(pStr.substring(0, 1));
			p.setColors(pStr.substring(1, 2));
			if (i == 0) {
				myHands.setP1(p);
			} else {
				myHands.setP1(p);
			}
		}
	}

	private void setFlopPList(String s) {
		flopPList = new ArrayList<P>();
		int idx = s.indexOf("*** FLOP *** [") + ("*** FLOP *** [").length();
		for (int i = 0; i < 3; i++) {
			String pStr = s.substring(idx + i * 3, idx + i * 3 + 2);
			P p = new P();
			p.setNums(pStr.substring(0, 1));
			p.setColors(pStr.substring(1, 2));
			flopPList.add(p);
		}
	}

	private void setTurnPList(String s) {
		turnPList = new ArrayList<P>();
		int idx = s.indexOf("] [") + "] [".length();
		for (int i = 0; i < 1; i++) {
			String pStr = s.substring(idx + i * 3, idx + i * 3 + 2);
			P p = new P();
			p.setNums(pStr.substring(0, 1));
			p.setColors(pStr.substring(1, 2));
			turnPList.add(p);
		}
		turnPList.addAll(flopPList);
	}

	private void setRiverPList(String s) {
		riverPList = new ArrayList<P>();
		int idx = s.indexOf("] [") + "] [".length();
		for (int i = 0; i < 1; i++) {
			String pStr = s.substring(idx + i * 3, idx + i * 3 + 2);
			P p = new P();
			p.setNums(pStr.substring(0, 1));
			p.setColors(pStr.substring(1, 2));
			riverPList.add(p);
		}
		riverPList.addAll(turnPList);
	}
}
